//
//  GouwucheViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/5.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "GouwucheViewController.h"

#import "GouwucheTableViewCell.h"
#import "TijiaodingdanViewController.h"


@interface GouwucheViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;
//头视图
@property (nonatomic, strong) UIView * view_headBg;
//结算栏
@property (nonatomic, strong) UIView * view_bottom;
@property (nonatomic, strong) UIButton * btn_quanxuan;
@property (nonatomic, strong) UILabel * price_quanxuan;
@property (nonatomic, strong) UIButton * btn_jiesuan;
@property (nonatomic, assign) BOOL is_qujiesuan;

//数据
@property (nonatomic, strong) NSMutableArray * arr_selectData;

@end

@implementation GouwucheViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.backgroundColor = [UIColor blackColor];
    
    _lblTitle.text = @"购物车";
    _lblTitle.textColor = [UIColor whiteColor];
    _lblTitle.font = [UIFont systemFontOfSize:19];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - 50) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    //头视图
    [self p_headView];
    self.tableView.tableHeaderView = self.view_headBg;
    
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[GouwucheTableViewCell class] forCellReuseIdentifier:@"cell_gouwuche"];
    
    //结算栏
    [self p_bottomView];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GouwucheTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_gouwuche" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //
    cell.btn_delete.tag = 200 + indexPath.row;
    [cell.btn_delete addTarget:self action:@selector(btn_deleteAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //减
    cell.btn_jian.tag = 400 + indexPath.row;
    [cell.btn_jian addTarget:self action:@selector(btn_jianAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //加
    cell.btn_add.tag = 600 + indexPath.row;
    [cell.btn_add addTarget:self action:@selector(btn_addAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //全选
    cell.btn_select.tag = 1000 + indexPath.row;
    [cell.btn_select addTarget:self action:@selector(btn_selectAction:) forControlEvents:(UIControlEventTouchUpInside)];

    
    [cell.btn_select setBackgroundImage:[UIImage imageNamed:@"weixuanzhong@2x.png"] forState:(UIControlStateNormal)];
    for (NSString * str_index in self.arr_selectData)
    {
        if([str_index integerValue] == indexPath.row)
        {
            [cell.btn_select setBackgroundImage:[UIImage imageNamed:@"xuanzhong@2x.png"] forState:(UIControlStateNormal)];
        }
    }
    
    return cell;
}

#pragma mark - 全选
- (void )btn_selectAction:(UIButton *)sender
{
    NSInteger count_x = sender.tag - 1000;
    NSLog(@"对第 %ld 个进行操作",(long)count_x);
    
    if([sender.currentBackgroundImage isEqual:[UIImage imageNamed:@"weixuanzhong@2x.png"]])
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"xuanzhong@2x.png"] forState:(UIControlStateNormal)];
        
        [self.arr_selectData addObject:[NSString stringWithFormat:@"%ld",(long)count_x]];
    }
    else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"weixuanzhong@2x.png"] forState:(UIControlStateNormal)];
        
        [self.arr_selectData removeObject:[NSString stringWithFormat:@"%ld",(long)count_x]];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}

#pragma mark - 点击删除
- (void )btn_deleteAction:(UIButton *)sender
{
    NSInteger count_x = sender.tag - 200;
    NSLog(@"删除第 %ld 个",(long)count_x);
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定从购物车内删除此物品" preferredStyle:(UIAlertControllerStyleAlert)];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action1];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"调取删除接口");
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            
            [self.tableView reloadData];
        });
        
    }];
    [alert addAction:action];
}

#pragma mark - 购物车加和减
//减
- (void )btn_jianAction:(UIButton *)sender
{
    NSInteger count_x = sender.tag - 400;
    NSLog(@"对第 %ld 个进行减少操作",(long)count_x);
}

//加
- (void )btn_addAction:(UIButton *)sender
{
    NSInteger count_x = sender.tag - 600;
    NSLog(@"对第 %ld 个进行增加操作",(long)count_x);
}

#pragma mark - 头视图
- (void )p_headView
{
    self.view_headBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    self.view_headBg.backgroundColor = [UIColor whiteColor];
    
    UIImageView * image_dianpu = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12.5, 25, 25)];
    image_dianpu.image = [UIImage imageNamed:@"dianpu@2x.png"];
    [self.view_headBg addSubview:image_dianpu];
    
    UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_dianpu.frame) + 10, 15, SCREEN_WIDTH - CGRectGetMaxX(image_dianpu.frame) - 20, 20)];
    detail.text = @"本APP所有商品由千金商城统一配送";
    detail.font = [UIFont systemFontOfSize:14];
    detail.textColor = navi_bar_bg_color;
    [self.view_headBg addSubview:detail];
    
    UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
    view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view_headBg addSubview:view_line];
}

#pragma mark - 结算栏
- (void )p_bottomView
{
    self.view_bottom = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 49 - 50, SCREEN_WIDTH, 50)];
    self.view_bottom.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.view_bottom];
    
    UIView * view_line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    view_line1.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view_bottom addSubview:view_line1];
    UIView * view_line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
    view_line2.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view_bottom addSubview:view_line2];
    
    //
    self.btn_quanxuan = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_quanxuan.frame = CGRectMake(10, 12.5, 25, 25);
    [self.btn_quanxuan setBackgroundImage:[UIImage imageNamed:@"weixuanzhong@2x.png"] forState:(UIControlStateNormal)];
    [self.view_bottom addSubview:self.btn_quanxuan];
    [self.btn_quanxuan addTarget:self action:@selector(btn_quanxuanAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UILabel * lbl_quanxuan = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.btn_quanxuan.frame) + 5, 15, 40, 20)];
    lbl_quanxuan.text = @"全选";
    lbl_quanxuan.font = [UIFont systemFontOfSize:15];
    [self.view_bottom addSubview:lbl_quanxuan];
    
    UILabel * lbl_heji = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lbl_quanxuan.frame) + 10, 15, 40, 20)];
    lbl_heji.text = @"合计:";
    lbl_heji.textColor = [UIColor grayColor];
    lbl_heji.font = [UIFont systemFontOfSize:15];
    [self.view_bottom addSubview:lbl_heji];
    
    //
    self.price_quanxuan = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lbl_heji.frame), 14, SCREEN_WIDTH - CGRectGetMaxX(lbl_heji.frame) - 5 - SCREEN_WIDTH / 4, 22)];
    self.price_quanxuan.textColor = [UIColor redColor];
    self.price_quanxuan.font = [UIFont systemFontOfSize:17];
    self.price_quanxuan.text = @"￥200.00";
    [self.view_bottom addSubview:self.price_quanxuan];
    
    self.btn_jiesuan = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_jiesuan.frame = CGRectMake(SCREEN_WIDTH - SCREEN_WIDTH / 4, 0, SCREEN_WIDTH / 4, 50);
    self.btn_jiesuan.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view_bottom addSubview:self.btn_jiesuan];
    [self.btn_jiesuan addTarget:self action:@selector(btn_jiesuanAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
//    //为不满足状态
//    self.btn_jiesuan.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    [self.btn_jiesuan setTitle:@"还差￥28" forState:(UIControlStateNormal)];
//    [self.btn_jiesuan setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//    self.is_qujiesuan = NO;
    
    //为满足状态
    self.btn_jiesuan.backgroundColor = navi_bar_bg_color;
    [self.btn_jiesuan setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.btn_jiesuan setTitle:@"去结算" forState:(UIControlStateNormal)];
    self.is_qujiesuan = YES;
}

#pragma mark - 底部栏全选
- (void )btn_quanxuanAction:(UIButton *)sender
{
    if([sender.currentBackgroundImage isEqual:[UIImage imageNamed:@"weixuanzhong@2x.png"]])
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"xuanzhong@2x.png"] forState:(UIControlStateNormal)];
    }
    else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"weixuanzhong@2x.png"] forState:(UIControlStateNormal)];
    }
}

#pragma mark - 去结算
- (void )btn_jiesuanAction:(UIButton *)sender
{
    if(self.is_qujiesuan == YES)
    {
//        NSLog(@"可以去结算");
        
        TijiaodingdanViewController * tijiaodingdanViewController = [[TijiaodingdanViewController alloc] init];
        [self showViewController:tijiaodingdanViewController sender:nil];
    }
    else
    {
        NSLog(@"还差多少钱");
        
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"还差多少钱,不能去马上结算" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:action];
    }
}

#pragma mark - 左滑删除
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定从购物车中删除该商品" preferredStyle:(UIAlertControllerStyleAlert)];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            
            [self.tableView reloadData];
        });
    }];
    
    [alert addAction:action1];
}

#pragma mark - 懒加载
- (NSMutableArray *)arr_selectData
{
    if(_arr_selectData == nil)
    {
        self.arr_selectData = [NSMutableArray array];
    }
    
    return _arr_selectData;
}













@end
